/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ZoneHaoYouRemarkActivity.java   
 * @Package com.jiyi.activity.zone   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-7-7 上午9:06:14   
 * @version V1.0     
 */  
package com.jiyi.activity.zone;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**   
 * @ClassName:  ZoneHaoYouRemarkActivity   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: HYM  
 * @date:   2017-7-7 上午9:06:14   
 *      
 */
public class ZoneHaoYouRemarkActivity extends ActivityBase{

	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;
	@BindView(R.id.etRemark)
	EditText etRemark;
	
	//进度条
	Dialog dialog;
	//备注名
	String remark_name;
	//好友id
	String id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zone_haoyou_remark);
		ButterKnife.bind(this);
		init();
	}
	
	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}
	
	 
	private void init() {
		getIntentData();
		setTop();
	}

	/**   
	 * @author: HYM 
	 * @Title: getIntentData   
	 * @Description: TODO(这里用一句话描述这个方法的作用)    void 
	 * @date: 2017-7-27
	 */  
	private void getIntentData() {
		id = getIntent().getStringExtra("id");
		remark_name = getIntent().getStringExtra("remark_name");
		if (!TextUtils.isEmpty(remark_name)) {
			etRemark.setText(remark_name);
		}
	}


	private void setTop() {
		tvBack.setText("");
		tvSearch.setCompoundDrawables(null, null, null, null);
		tvSearch.setText(getResources().getString(R.string.save));		
		tvTitle.setText("设置备注名");
	}


	@OnClick({ R.id.tvBack,R.id.tvSearch})
	public void onClick(View veiw) {
		Intent intent = null;
		switch (veiw.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvSearch:
			//执行设置备注名
			if (TextUtils.isEmpty(etRemark.getText())) {
				ToastUtil.getInstances().showMsg("备注名不能为空");
				return;
			}
			
			doPost(id, etRemark.getText().toString());
			break;
		default:
			break;
		}
	}
	
	/**   
	 * @author: HYM 
	 * @Title: doPost   
	 * @Description: TODO(执行备注修改)   
	 * @param id 好友id
	 * @param remark_name 备注名
	 * @date: 2017-7-27
	 */  
	void doPost(String id,final String remark_name) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", id);
		params.put("remark_name", remark_name);
		
		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, WebApi.ZONE_HAOYOU_REMARK, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
								Intent intent = new Intent();
								intent.putExtra("remark_name", remark_name);
								setResult(RESULT_OK, intent);
								finish();
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}
}
